<?
class zoekenController extends inabuurt_controller_action{
	
	public function indexAction(){
		require_once('./application/models/zoekenModel.php');
		$this -> view -> setTpl('zoeken.php');	
		$crumbs = $this -> view -> breadcrumb;
		$this -> view -> addStyle(view::PATH_TO_STYLE.'zoeken.css');
			$form = new form('frm_zoeken',form::POST);
			$zoek = new form_element_textField('frm_search_input');		
			$form -> addElement($zoek);
			$foto = '';
			$video = '';
			$albums = '';
			
			
			if ($this -> request -> getParam(0) != NULL && $this -> request -> getParam(1) == NULL || $form -> isSend()){
			
				if ($this -> request -> getParam(0) != NULL){
				
					$this -> view -> overzicht = true;
					
					$item = zoekenModel::getInstance($this -> conn);
					
					$zoeken = $this -> request -> getParam(0);
				
					if(empty($zoeken)){
						$this -> view -> nothing= true;
						$this -> view -> render();
						die();
				
					}		
				
					
				}
			
			
				if($form -> isSend()){
					$this -> view -> overzicht = true;
					
					$item = zoekenModel::getInstance($this -> conn);
					
					$zoeken = $zoek -> getSend();
				
					if(empty($zoeken)){
						$this -> view -> nothing= true;
						$this -> view -> render();
						die();
					}
				
				}
			
			
			$crumbs[] = array($zoeken, '');
			$items = $item -> getItems($zoeken);
			$berichten = $item -> getBerichten($zoeken);
			$zoekertjes = $item -> getZoekertjes($zoeken);
			$nieuws = $item -> getNieuws($zoeken);
			$activiteiten = $item -> getActiviteiten($zoeken);
			
			foreach($items as $value)
			{
				switch($value['type'])
				{
					case 'foto':
						$foto = $value['id'];
						break;
					case 'video':
						$video = $value['id'];
						break;
					case 'album':
						$albums = $value['id'];
						break;
				}
			}
			$this -> view -> term = $zoeken;
			$this -> view -> fotos = $foto;
			$this -> view -> videos = $video;
			$this -> view -> albums = $albums; 
			$this -> view -> bericht = $berichten;
			$this -> view -> nieuws = $nieuws;
			$this -> view -> activiteiten = $activiteiten;
			$this -> view -> zoekertjes = $zoekertjes;
			
		
		}	
		
			if ($this -> request -> getParam(1) != NULL)
			{
				switch($this -> request -> getParam(0))
				{
					case 'tekstberichten':
						$crumbs[] = array('teksberichten', '');
						$this -> loadBerichten();
						break;
					case 'fotos':
						$crumbs[] = array("foto's", '');
						$this -> loadFotos();
						break;
					case 'albums':
						$this -> loadAlbums();
						$crumbs[] = array('albums', '');
						break;
					case 'nieuws':
						$this -> loadNieuws();
						$crumbs[] = array('nieuws', '');
						break;
					case 'videos':
					$crumbs[] = array('videos', '');
						$this -> loadVideos();
						break;
					case 'activiteiten':
					$crumbs[] = array('activiteiten', '');
						$this -> loadActivi();
						break;
					case 'zoekertjes':
						$crumbs[] = array('zoekertjes', '');
						$this -> loadZoekertjes();
						break;
				}	
				
				$this -> view -> term =  $this -> request -> getParam(1);
				
		}
		
		
		$this -> view -> breadcrumb = $crumbs;
		$this -> view -> render();
	}
		
		
		
		
	public function	loadBerichten(){
	
		$myModel = zoekenModel::getInstance($this -> conn);
		$returnArray = array();
		$zoeken = $myModel -> getSearchedBerichten($this -> request -> formatParam(1));
		foreach($zoeken as $value){
			$returnArray[] = array('item' => $value, 'link' => '/bewonersgroepen/'. $value -> geb_naam . '/tekstberichten/' .$this -> view -> encodeForUrl($value -> title) .'/'.$value -> id .'/' );
		}
		$this -> view -> zoeken = $returnArray;
		
	}
	public function	loadFotos(){
		
		$myModel = zoekenModel::getInstance($this -> conn);
		$returnArray = array();
		$zoeken = $myModel -> getSearchedFotos($this -> request -> formatParam(1));
		foreach($zoeken as $value){
			$returnArray[] = array('item' => $value, 'link' => '/bewonersgroepen/'. $value -> geb_naam . '/fotos/' .$this -> view -> encodeForUrl($value -> foto_album_naam) .'/'.$value -> foto_album_id .'/' );
		}
		$this -> view -> zoeken = $returnArray;
	
	}
	public function	loadAlbums(){
		$myModel = zoekenModel::getInstance($this -> conn);
		$returnArray = array();
		$zoeken = $myModel -> getSearchedAlbums($this -> request -> formatParam(1));
		foreach($zoeken as $value){
			switch($value -> album_type)
			{	case 0:
					$returnArray[] = array('item' => $value, 'link' => '/bewonersgroepen/'. $value -> geb_naam . '/fotos/');
					break;
				case 1:
					$returnArray[] = array('item' => $value, 'link' => '/bewonersgroepen/'. $value -> geb_naam . '/video/');
					break;
			}
		}
		$this -> view -> zoeken = $returnArray;
	}
	public function	loadNieuws(){
		$myModel = zoekenModel::getInstance($this -> conn);
		$returnArray = array();
		$zoeken = $myModel -> getSearchedNieuws($this -> request -> formatParam(1));
		foreach($zoeken as $value){
			$returnArray[] = array('item' => $value, 'link' => '/nieuws/'.$this -> view -> encodeForUrl($value -> title) .'/'.$value -> id .'/' );
		}
		$this -> view -> zoeken = $returnArray;
	}
	public function loadVideos(){
		$myModel = zoekenModel::getInstance($this -> conn);
		$returnArray = array();
		$zoeken = $myModel -> getSearchedVideos($this -> request -> formatParam(1));
		foreach($zoeken as $value){
			$returnArray[] = array('item' => $value, 'link' => '/bewonersgroepen/'. $value -> geb_naam . '/videos/' .$this -> view -> encodeForUrl($value -> album_naam) .'/'.$value -> album_id .'/'. $value -> title .'/'.$value -> id.'/' );
		}
		$this -> view -> zoeken = $returnArray;
	}
	public function loadActivi(){
		$myModel = zoekenModel::getInstance($this -> conn);
		$returnArray = array();
		$zoeken = $myModel -> getSearchedActivi($this -> request -> formatParam(1));
		foreach($zoeken as $value){
			list($day, $month, $year) = split('[/.-]', $value -> act_date);
			if (substr($month,0,1) == 0){
				$month = substr($month,1,1);
			}
			$returnArray[] = array('item' => $value, 'link' => '/kalender/activiteiten/'.$year.'/'.$month.'/'.$day.'/');
		}
		$this -> view -> zoeken = $returnArray;
		
		
		
		
		
	}
	public function loadZoekertjes(){
		$myModel = zoekenModel::getInstance($this -> conn);
		$returnArray = array();
		$zoeken = $myModel -> getSearchedZoekertjes($this -> request -> formatParam(1));
		foreach($zoeken as $value){
			switch($value -> zoeker_type)
			{	case 0:
					$returnArray[] = array('item' => $value, 'link' => '/bewonersgroepen/'. $value -> geb_naam . '/zoekertjes/aanvragen/'.$this -> view -> encodeForUrl($value -> title).'/'.$value -> id.'/');
					break;
				case 1:
					$returnArray[] = array('item' => $value, 'link' => '/bewonersgroepen/'. $value -> geb_naam . '/zoekertjes/aanbiedingen/'.$this -> view -> encodeForUrl($value -> title).'/'.$value -> id.'/');
					break;
			}
		}
		$this -> view -> zoeken = $returnArray;
	}
		
		
		
			
}



?>